{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "620899d8",
   "metadata": {},
   "source": [
    "# 优化ASR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b9e30b64",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 百度秘钥\n",
    "API_KEY = 'B7iY9oO90ZsAlsiGhyhVYbL9'\n",
    "SECRET_KEY = 'zb1TxZS3oWxH3QO7NTF9pXweIrzaE2EU'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "92b5b560",
   "metadata": {},
   "outputs": [],
   "source": [
    "from bdasr import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "12fb1e89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.9a699ef04dae78faef9aa9da39cf3755.2592000.1686830681.282335-33296583'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "z_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6946ffff",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "569dc20e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.207359\n",
      "{\"err_msg\":\"request pv too much\",\"err_no\":3305,\"sn\":\"173066934041684238942\"}\n",
      "\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "eval() arg 1 must be a string, bytes or code object",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [9]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m asr_output \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43meval\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43masr\u001b[49m\u001b[43m(\u001b[49m\u001b[43mz_token\u001b[49m\u001b[43m,\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m1.wav\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mresult\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;241m0\u001b[39m]\n\u001b[0;32m      2\u001b[0m asr_output\n",
      "\u001b[1;31mTypeError\u001b[0m: eval() arg 1 must be a string, bytes or code object"
     ]
    }
   ],
   "source": [
    "asr_output = eval(asr(z_token,'1.wav'))['result'][0]\n",
    "asr_output"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e7d9f6d",
   "metadata": {},
   "source": [
    "# 连接openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6d515851",
   "metadata": {},
   "outputs": [],
   "source": [
    "import opchat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1dca6c37",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'asr_output' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [11]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m chat \u001b[38;5;241m=\u001b[39m opchat\u001b[38;5;241m.\u001b[39mopenai_chat(\u001b[43masr_output\u001b[49m)\n\u001b[0;32m      2\u001b[0m chat\n",
      "\u001b[1;31mNameError\u001b[0m: name 'asr_output' is not defined"
     ]
    }
   ],
   "source": [
    "chat = opchat.openai_chat(asr_output)\n",
    "chat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "296a4efd",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\conda\\lib\\site-packages\\pydub\\utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work\n",
      "  warn(\"Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work\", RuntimeWarning)\n"
     ]
    }
   ],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('1.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a47e7e0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
